package com.wangzy.game;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ProjectName: SportBS
 * @Date: 2022/8/19
 * @Desc:精灵容器，包含没有死亡的所有精灵
 */
public class SpriteContainer extends ConcurrentHashMap<String, Sprite> {

    private IGameCanvas iGameCanvas;

    public SpriteContainer(IGameCanvas iGameCanvas) {
        super();
        this.iGameCanvas = iGameCanvas;
    }

    void addSprit(String key, Sprite sprite) {
        sprite.setKey(key);
        sprite.setSpriteContainer(this);
        put(key, sprite);
    }

    void addSprit(Sprite sprite) {
        sprite.setSpriteContainer(this);
        addSprit(sprite.getKey(), sprite);
    }

    public Set<Entry<String, Sprite>> enumAllSprite() {
        Set<Entry<String, Sprite>> allEle = entrySet();
        return allEle;
    }


    Comparator<? super Sprite> comparator=new Comparator<Sprite>() {
        @Override
        public int compare(Sprite o1, Sprite o2) {
            int x=o1.getZorderIndex()-o2.getZorderIndex();
            if(x<0){
                return -1;
            }else if(x>0){
                return 1;
            }else{
                return 0;
            }

        }
    };
    public ArrayList<Sprite> allSprite(){
        Set<Entry<String, Sprite>> sets = entrySet();
        ArrayList<Sprite>  sprites=new ArrayList<>(sets.size());
        for (Entry<String, Sprite> entr : sets) {
            Sprite sp = entr.getValue();
            sprites.add(sp);
        }
        Collections.sort(sprites,comparator);
        return sprites;
    }

}